package com.dm.logistics.basticmanage.service.impl;

import com.dm.logistics.basticmanage.controller.IndexController;
import com.dm.logistics.basticmanage.mapper.BasticMapper;
import com.dm.logistics.basticmanage.service.BasticService;
import com.dm.logistics.common.util.ResultJsonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.*;

@Service
public class BasticServiceImpl implements BasticService{
    @Autowired
    private ResultJsonUtil resultJsonUtil;
    @Autowired
    private BasticMapper basticMapper;


    /**
         * @Description 查询所有固定资产基础信息
         * @Author zhangxue
         * @Date 14:52 2018/5/5
         * @Param
         * @retrun
         *
     * @param param*/
    @Override
    public Map<String, Object> queryAllBasicEqu(Map<String, Object> param) {
        String parentstore = param.get("parentstore")!=null?param.get("parentstore").toString():null;
        if(parentstore.equals("数信网办公室")||parentstore.equals("集团办公室")){

        }else{
            param.put("store",parentstore);
        }
        String inputStore = param.get("store")!=null?param.get("store").toString():null;
        if("全部".equals(inputStore)){
            param.put("store","");
        }
        List<Map<String,Object>> resultList = basticMapper.queryAllBasicEqu(param);
        Double BuyInMoneyTotal = 0.0;
        for(int i=0;i<resultList.size();i++){
            String StartTimeStr = resultList.get(i).get("StartTime")!=null?resultList.get(i).get("StartTime").toString():null;
            String[] StartTimeStrArr = StartTimeStr.split(" ");
            resultList.get(i).put("StartTime",StartTimeStrArr[0]);
            String EndTimeStr = resultList.get(i).get("EndTime")!=null?resultList.get(i).get("EndTime").toString():null;
            String[] EndTimeStrArr = EndTimeStr.split(" ");
            resultList.get(i).put("EndTime",EndTimeStrArr[0]);
            String BuyInTimeStr = resultList.get(i).get("BuyInTime")!=null?resultList.get(i).get("BuyInTime").toString():null;
            String[] BuyInTimeStrArr = BuyInTimeStr.split(" ");
            resultList.get(i).put("BuyInTime",BuyInTimeStrArr[0]);

            String LastConseTimeStr = resultList.get(i).get("LastConseTime")!=null?resultList.get(i).get("LastConseTime").toString():null;
            if(LastConseTimeStr!=null){
                String[] LastConseTimeStrArr = LastConseTimeStr.split(" ");
                resultList.get(i).put("LastConseTime",LastConseTimeStrArr[0]);
            }
            String NextConseTimeStr = resultList.get(i).get("NextConseTime")!=null?resultList.get(i).get("NextConseTime").toString():null;
            if(NextConseTimeStr!=null){
                String[] NextConseTimeStrArr = NextConseTimeStr.split(" ");
                resultList.get(i).put("NextConseTime",NextConseTimeStrArr[0]);
            }
            Double tempBuyInMoney = Double.valueOf(resultList.get(i).get("BuyInMoney").toString());
            BuyInMoneyTotal += tempBuyInMoney;
        }

        Map<String,String> count = basticMapper.queryBasicListCount(param);
        JSONArray Js = JSONArray.fromObject(resultList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", count.get("total"));
        jsonObject.put("rows", Js.toString());
        //添加页脚合计行
        Map<String,Object> tempBuyInMoneyTotal = new HashMap<String, Object>();
        tempBuyInMoneyTotal.put("BuyInTime","当前页合计");
        tempBuyInMoneyTotal.put("BuyInMoney",BuyInMoneyTotal);
        List<Map<String,Object>> footList = new ArrayList<Map<String,Object>>();
        footList.add(tempBuyInMoneyTotal);

        Double CostTotal2 = 0.0;
        List<Map<String,Object>> resultList2 = basticMapper.queryAllBasicEqu2(param);
        for(int i=0;i<resultList2.size();i++){
            Double CostTotalTemp = Double.valueOf( resultList2.get(i).get("BuyInMoney")!=null? resultList2.get(i).get("BuyInMoney").toString():"0");
            CostTotal2 += CostTotalTemp;
        }
        Map<String,Object> tempCostTotal2 = new HashMap<String, Object>();
        tempCostTotal2.put("BuyInTime","总计");
        tempCostTotal2.put("BuyInMoney",CostTotal2);
        footList.add(tempCostTotal2);

        JSONArray footJs = JSONArray.fromObject(footList);
        jsonObject.put("footer",footJs.toString());
        Map<String,Object> tempMap = new HashMap<String, Object>();
        tempMap.put("serviceList",jsonObject);
        return tempMap;
    }

    /**
     * @Description 查询所有固定资产基础信息
     * @Author zhangxue
     * @Date 14:52 2018/5/5
     * @Param
     * @retrun
     *
     * @param param*/
    @Override
    public Map<String, Object> queryAllBasicEqu3(Map<String, Object> param) {
        String parentstore = param.get("parentstore")!=null?param.get("parentstore").toString():null;
        if(parentstore.equals("数信网办公室")||parentstore.equals("集团办公室")){

        }else{
            param.put("store",parentstore);
        }
        String inputStore = param.get("store")!=null?param.get("store").toString():null;
        if("全部".equals(inputStore)){
            param.put("store","");
        }
        List<Map<String,Object>> resultList = basticMapper.queryAllBasicEqu(param);
        Double BuyInMoneyTotal = 0.0;
        for(int i=0;i<resultList.size();i++){
            String StartTimeStr = resultList.get(i).get("StartTime")!=null?resultList.get(i).get("StartTime").toString():null;
            String[] StartTimeStrArr = StartTimeStr.split(" ");
            resultList.get(i).put("StartTime",StartTimeStrArr[0]);
            String EndTimeStr = resultList.get(i).get("EndTime")!=null?resultList.get(i).get("EndTime").toString():null;
            String[] EndTimeStrArr = EndTimeStr.split(" ");
            resultList.get(i).put("EndTime",EndTimeStrArr[0]);
            String BuyInTimeStr = resultList.get(i).get("BuyInTime")!=null?resultList.get(i).get("BuyInTime").toString():null;
            String[] BuyInTimeStrArr = BuyInTimeStr.split(" ");
            resultList.get(i).put("BuyInTime",BuyInTimeStrArr[0]);

            String LastConseTimeStr = resultList.get(i).get("LastConseTime")!=null?resultList.get(i).get("LastConseTime").toString():null;
            if(LastConseTimeStr!=null){
                String[] LastConseTimeStrArr = LastConseTimeStr.split(" ");
                resultList.get(i).put("LastConseTime",LastConseTimeStrArr[0]);
            }
            String NextConseTimeStr = resultList.get(i).get("NextConseTime")!=null?resultList.get(i).get("NextConseTime").toString():null;
            if(NextConseTimeStr!=null){
                String[] NextConseTimeStrArr = NextConseTimeStr.split(" ");
                resultList.get(i).put("NextConseTime",NextConseTimeStrArr[0]);
            }
            Double tempBuyInMoney = Double.valueOf(resultList.get(i).get("BuyInMoney").toString());
            BuyInMoneyTotal += tempBuyInMoney;
        }

        Map<String,String> count = basticMapper.queryBasicListCount(param);
        JSONArray Js = JSONArray.fromObject(resultList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", count.get("total"));
        jsonObject.put("rows", Js.toString());
        //添加页脚合计行
        Map<String,Object> tempBuyInMoneyTotal = new HashMap<String, Object>();
        tempBuyInMoneyTotal.put("BuyInTime","当前页合计");
        tempBuyInMoneyTotal.put("BuyInMoney",BuyInMoneyTotal);
        List<Map<String,Object>> footList = new ArrayList<Map<String,Object>>();
        footList.add(tempBuyInMoneyTotal);

        Double CostTotal2 = 0.0;
        List<Map<String,Object>> resultList2 = basticMapper.queryAllBasicEqu2(param);
        for(int i=0;i<resultList2.size();i++){
            Double CostTotalTemp = Double.valueOf( resultList2.get(i).get("BuyInMoney")!=null? resultList2.get(i).get("BuyInMoney").toString():"0");
            CostTotal2 += CostTotalTemp;
        }
        Map<String,Object> tempCostTotal2 = new HashMap<String, Object>();
        tempCostTotal2.put("BuyInTime","总计");
        tempCostTotal2.put("BuyInMoney",CostTotal2);
        footList.add(tempCostTotal2);

        JSONArray footJs = JSONArray.fromObject(footList);
        jsonObject.put("footer",footJs.toString());
        Map<String,Object> tempMap = new HashMap<String, Object>();
        tempMap.put("serviceList",jsonObject);
        return tempMap;
    }

    /**
     * @Description  通过设备id查询设备维修历程信息
     * @Author zhangxue
     * @Date 15:17 2018/5/7
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryBasicEquCourseById(Map<String, Object> map) {
        List<Map<String,Object>> resultList = basticMapper.queryBasicEquCourseByIdMapper(map);

        for(int i=0;i<resultList.size();i++){
            String ConseTimeStr = resultList.get(i).get("ConseTime")!=null?resultList.get(i).get("ConseTime").toString():null;
            if(ConseTimeStr==null){
                resultList.get(i).put("ConseTime",null);
            }else{
                String[] ConseTimeStrArr = ConseTimeStr.split(" ");
                resultList.get(i).put("ConseTime",ConseTimeStrArr[0]);
            }
            String ConseType = resultList.get(i).get("ConseType")!=null?resultList.get(i).get("ConseType").toString():null;
            if("1".equals(ConseType)){
                ConseType = "定期维保";
            }else if("2".equals(ConseType)){
                ConseType = "更换备件";
            }else if("3".equals(ConseType)){
                ConseType = "报废更换";
            }else{
                ConseType = "定期维保";
            }
            resultList.get(i).put("ConseType",ConseType);
        }
        Map<String,Object> resultMap = resultJsonUtil.getResultJsonMap(resultList);

        return resultMap;
    }

    /**
         * @Description 通过id查询资产信息
         * @Author zhangxue
         * @Date 10:35 2018/5/8
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> queryBasicEquById(Map<String, Object> map) {
        Map<String,Object> resultMap = basticMapper.queryBasicEquByIdMapper(map);
        if(resultMap!=null){
            String StartTimeStr = resultMap.get("StartTime")!=null?resultMap.get("StartTime").toString():null;
            resultMap.put("StartTime",StartTimeStr);
            String EndTimeStr = resultMap.get("EndTime")!=null?resultMap.get("EndTime").toString():null;
            resultMap.put("EndTime",EndTimeStr);
            String BuyInTimeStr = resultMap.get("BuyInTime")!=null?resultMap.get("BuyInTime").toString():null;
            resultMap.put("BuyInTime",BuyInTimeStr);

            String LastConseTimeStr = resultMap.get("LastConseTime")!=null?resultMap.get("LastConseTime").toString():null;
            resultMap.put("LastConseTime",LastConseTimeStr);
            String NextConseTimeStr = resultMap.get("NextConseTime")!=null?resultMap.get("NextConseTime").toString():null;
            resultMap.put("NextConseTime",NextConseTimeStr);
            Map<String,Object> tempMap = new HashMap<String,Object>();
            tempMap = basticMapper.queryEquConseCourseByDocNo(map);
            if(tempMap!=null){
                String temp = tempMap.get("ConseTime")!=null?tempMap.get("ConseTime").toString():null;
                resultMap.put("ConseTime",temp);
                temp = tempMap.get("ConseReason")!=null?tempMap.get("ConseReason").toString():null;
                resultMap.put("ConseReason",temp);
                temp = tempMap.get("ConseResult")!=null?tempMap.get("ConseResult").toString():null;
                resultMap.put("ConseResult",temp);
                temp = tempMap.get("Cost")!=null?tempMap.get("Cost").toString():null;
                resultMap.put("Cost",temp);
            }
        }
        return resultMap;
    }

    /**
         * @Description 保存新增资产信息
         * @Author zhangxue
         * @Date 9:54 2018/5/9
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveBasicEqu(Map<String, Object> map) {
        int i = basticMapper.saveBasicEquMapper(map);
        if(i==1){
            map.put("status","保存成功");
        }else{
            map.put("status","保存失败");
        }
        return map;
    }

    /**
         * @Description 将图片路径存入数据库中
         * @Author zhangxue
         * @Date 10:05 2018/5/12
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveEquImageInfos(Map<String, Object> imageParam) {
        int i = basticMapper.saveEquImageInfosMapper(imageParam);
        Map<String,Object> tempMap = new HashMap<String,Object>();
        if(i==1){
            tempMap.put("status","1");
        }else{
            tempMap.put("status","0");
        }
        return tempMap;
    }
    /**
         * @Description  根据资产编号查询资产图片url
         * @Author zhangxue
         * @Date 10:53 2018/5/12
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> queryImageUrlByEquNo(Map<String, Object> map) {
        List<Map<String,Object>> urlList = basticMapper.queryImageUrlByEquNoMapper(map);
        Map<String,Object> tempMap = new HashMap<String,Object>();
        if(urlList!=null&!"[]".equals(urlList.toString())){
            for(int i=0;i<urlList.size();i++){
                String filePath = "/"+urlList.get(i).get("ImageUrl");
                filePath = filePath.substring(1);
                tempMap.put("ImageUrl"+(i+1),filePath);
            }
        }
        return tempMap;
    }

    /**
         * @Description 通过id删除单条数据
         * @Author zhangxue
         * @Date 15:15 2018/5/12
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> delBasicEquById(Map<String, Object> map) {
        String ID = map.get("ID")!=null?map.get("ID").toString():null;
        String EquNo = map.get("EquNo")!=null?map.get("EquNo").toString():null;
        Map<String,Object> tempMap = new HashMap<String,Object>();
        if(ID.equals("undefined")&&EquNo.equals("undefined")){
            tempMap.put("Status","-2");
            return tempMap;
        }
        int i = basticMapper.delBasicEquByIdMapper(map);
        if(i==1){
            int j = basticMapper.delEquImgByEquNoMapper(map);
            if(j>0){
                tempMap.put("Status","1");
            }

        }else {
            tempMap.put("Status","0");
        }
        return tempMap;
    }

    /**
         * @Description 通过资产编号修改资产基本信息
         * @Author zhangxue
         * @Date 17:05 2018/5/12
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> updateBasicEquByEquNo(Map<String, Object> map) {
        Map<String, Object> tempMap = new HashMap<String, Object>();
            URL path = IndexController.class.getResource("/static/images");
            if (map.get("EquNo") != null & !"".equals(map.get("EquNo").toString())) {
                String filePath = path.getPath() + "/" + map.get("EquNo").toString();
                filePath = filePath.substring(1);
                    int i = basticMapper.updateBasicEquByEquNoMapper(map);
                    if (i == 1) {
                        tempMap.put("Status", "1");
                    } else {
                        tempMap.put("Status", "0");
                    }
                }
        return tempMap;
    }

    /**
         * @Description 通过资产编号查看是否存在相同资产编号的资产
         * @Author zhangxue
         * @Date 18:46 2018/5/16
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> queryBasicEquByEquNo(Map<String, Object> map) {
        Map<String,Object> tempMap = basticMapper.queryBasicEquByEquNoMapper(map);
        String count = tempMap.get("total")!=null?tempMap.get("total").toString():null;
        Map<String,Object> statusMap = new HashMap<String, Object>();
        if(!"0".equals(count)){
            statusMap.put("status","0");
        }else{
            statusMap.put("status","1");
        }
        return statusMap;
    }

    /**
     * @Description 导出Excel
     * @Author zhangxue
     * @Date 15:01 2018/6/14
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> CreateAssessExcel(Map<String, Object> param) {
        String store = param.get("store")!=null?param.get("store").toString():null;
        List<Map<String,Object>> resultList = basticMapper.QueryExcelByConditionMapper(param);
       System.out.println("back:"+param.toString());
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data",resultList);
        return resultMap;
    }
}
